package com.jackstraw.jack_base.util.exception;

import com.jackstraw.jack_base.util.ObjectUtil;
import com.jackstraw.jack_base.util.StringUtil;
import com.jackstraw.jack_base.util.entity.response.ResultInterface;
import com.jackstraw.jack_base.util.exception.enums.ExceptionEnum;
import lombok.Data;

/**
 * 业务服务异常对象
 *
 * @author zhaozhao
 */
@Data
public class BadException extends RuntimeException {
    /**
     * 序列化号
     */
    private static final long serialVersionUID = 1L;
    /**
     * 错误码
     */
    protected String errorCode;
    /**
     * 错误信息
     */
    protected String errorMsg;


    /**
     * 堆栈信息输出标志
     * <p>
     * 当创建对象是，传入了异常，则，抛出
     */
    protected Boolean flag = Boolean.FALSE;

    /**
     * 无参构造
     */
    public BadException() {
        super();
    }

    /**
     * 有参构造
     *
     * @param errorKey errorKey
     * @param obj      obj
     */
    public BadException(String errorKey, Object... obj) {
        this(ExceptionUtil.getErrCode(errorKey), ExceptionUtil.getErrMsg(errorKey, obj));
    }

    /**
     * 有参构造
     *
     * @param resultInterface errorInfoInterface
     */
    public BadException(ResultInterface resultInterface) {
        super(resultInterface.getMoreMsg());
        this.setErrorCode(resultInterface.getCode());
        this.setErrorMsg(resultInterface.getMoreMsg());
    }

    /**
     * 有参构造
     *
     * @param resultInterface errorInfoInterface
     * @param cause           cause
     */
    public BadException(ResultInterface resultInterface, Throwable cause) {
        super(resultInterface.getMoreMsg(), cause);
        this.setErrorCode(resultInterface.getCode());
        this.setErrorMsg(resultInterface.getMoreMsg());
        this.setFlag(Boolean.TRUE);
    }

    /**
     * 有参构造
     *
     * @param errorKey errorKey
     */
    public BadException(String errorKey) {
        this(ExceptionUtil.getErrCode(errorKey), ExceptionUtil.getErrMsg(errorKey));
    }

    /**
     * 有参构造
     *
     * @param errorCode errorCode
     * @param errorMsg  errorMsg
     */
    public BadException(String errorCode, String errorMsg) {
        super(errorMsg);
        this.setErrorCode(errorCode);
        this.setErrorMsg(errorMsg);
    }

    /**
     * 有参构造
     *
     * @param errorCode
     * @param errorMsg
     * @param obj
     */
    public BadException(String errorCode, String errorMsg, Object... obj) {
        super(StringUtil.placeholderAnalysis(errorMsg, obj));
        this.setErrorCode(errorCode);
        this.setErrorMsg(StringUtil.placeholderAnalysis(errorMsg, obj));
    }

    /**
     * 有参构造
     *
     * @param errorCode errorCode
     * @param errorMsg  errorMsg
     * @param cause     cause
     */
    public BadException(String errorCode, String errorMsg, Throwable cause) {
        super(errorMsg, cause);
        this.setErrorCode(errorCode);
        this.setErrorMsg(errorMsg);
        this.setFlag(Boolean.TRUE);
    }

    /**
     * 有参构造
     *
     * @param errorCode
     * @param errorMsg
     * @param cause
     * @param obj
     */
    public BadException(String errorCode, String errorMsg, Throwable cause, Object... obj) {
        super(StringUtil.placeholderAnalysis(errorMsg, obj), cause);
        this.setErrorCode(errorCode);
        this.setErrorMsg(StringUtil.placeholderAnalysis(errorMsg, obj));
        this.setFlag(Boolean.TRUE);
    }


    /**
     * 构建errorCode
     *
     * @param exceptionEnum
     * @param errorCode
     * @return
     */
    public static String bulidErrorCode(ExceptionEnum exceptionEnum, String errorCode) {
        return exceptionEnum.getErrorSubCode(errorCode);
    }


    /**
     * 拿到错误信息
     *
     * @return
     */
    @Override
    public String getMessage() {
        return errorMsg;
    }

    /**
     * fillInStackTrace
     * <p>
     * 自定义异常，不打印堆栈信息
     *
     * @return Throwable
     */
    @Override
    public Throwable fillInStackTrace() {
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(this.getFlag()) || this.getFlag()) {
            return super.fillInStackTrace();
        }
        return this;
    }

    @Override
    public String toString() {
        return "BadException: " + errorCode + "->" + errorMsg;

    }
}
